Utility for identifying differences between two Java objects

ABSTRACT

A utility for comparing two objects in an object-oriented operating system that also records the differences so that they may be put into human-readable form. In one exemplary embodiment of this invention, two JAVA objects are compared by calling one of the equality methods. If the selected equality method indicates that there is a difference between the two objects, then get . . . ( ) methods of each object are invoked in turn. The results get . . . ( ) methods are compared. If there are differences, the differences are stored in an XML document. The get . . . ( ) method is recursively invoked until the Class of the result has no more get . . . ( ) methods to decompose.

CROSS-REFERENCE TO RELATED APPLICATION

This patent application is related to and claims the benefit ofProvisional U.S. Patent Application No. 60/467,340 filed May 5, 2003,which application is hereby incorporated herein by reference in itsentirety.

RESERVATION OF RIGHTS IN COPYRIGHTED MATERIAL

A portion of the disclosure of this patent document contains materialwhich is subject to copyright protection. The copyright owner has noobjection to the facsimile reproduction by anyone of the patent documentor the patent disclosure, as it appears in the Patent and TrademarkOffice patent file or records, but otherwise reserves all copyrightrights whatsoever.

FIELD OF THE INVENTION

This invention relates to the field of object-oriented software, and,more specifically, to a utility for differentiating between two objectsand, advantageously, storing the differences for display.

BACKGROUND OF THE INVENTION

Differentiating between two “things” of a similar nature is a problemfrequently addressed in the area of computer science. In fact, manyprogrammers take it for granted that a differentiation utility exists intheir particular area. For example, the UNIX operating system includes a“diff” function that shows, line by line, the differences between twofiles. Microsoft Word includes a “Reviewing” taskbar that shows changesbetween and among versions of a document. In fact, many operatingsystems, word processors, operating environments include a procedure,function, facility, etc. that determines the differences between twoitems.

In more modern, object-oriented software areas, however, thisdifferentiation utility is not always a part of the system. For example,JAVA, a programming language and operating system developed by SunMicrosystems, only provides a way of comparing the equality of twoobjects of the same Class. There is no universally defined method todetermine the differences between two objects.

Therefore, a problem exists in the current object-oriented software artto differentiate between two objects of the same Class, especially inJAVA.

SUMMARY OF THE INVENTION

This problem is solved and a technical advance is achieved in the art bya system and method that provides a utility for comparing two objects inan object-oriented operating system and recording the differences sothat they may be put into human-readable form.

Advantageously, the objects to be compared are JAVA objects and thedifferences are recorded in XML format, which is easily transformed intohuman-understandable form (e.g., transform the XML into web pages usingXSL).

In one exemplary embodiment of this invention, two JAVA objects arecompared by calling one of the JAVA equality methods. The equalitymethod Comparable.compareTo( ) is used if the objects to compare areinstances of Comparable. Otherwise, the Objects.equals( ) method isused. If the selected equality method indicates that there is adifference between the two objects, then the get . . . ( ) methods ofeach object are invoked in turn. The results of the get . . . ( )methods are compared. If there are differences, the differences arestored in an XML document. The get . . . ( ) method is recursivelyinvoked until the Class of the result indicates no further get . . . ( )methods to decompose.

This exemplary embodiment of this invention generates an XML documentthat shows the difference(s) between the two objects. To show thecontext of a field that is different between the two objects, the fullClass hierarchy is described with attributes showing which parts aredifferent and which parts are the same. The value of each field isdisplayed by a toString( ) call when the type of the field cannot bedecomposed by further get . . . ( ) calls.

BRIEF DESCRIPTION OF THE DRAWINGS

A more complete understanding of this invention may be obtained from aconsideration of this specification taken in conjunction with thedrawings, in which:

FIG. 1 is a Warnier/Orr diagram illustrating the organization of anexemplary embodiment of this invention;

FIG. 2 is an element type diagram illustrating the XML document typedeclarations in accordance with the exemplary embodiment of thisinvention;

FIG. 3 is a sample Class definition for the purpose of illustratingoperation of the exemplary embodiment of this invention;

FIG. 4 is a test case using the sample Class of FIG. 3;

FIG. 5 is the XML generated by the exemplary embodiment of thisinvention on the test case of FIG. 4; and

FIGS. 6-13 comprise a program listing of an exemplary objectdifferentiation program, called “objectDiff.”

DETAILED DESCRIPTION

The Detailed Description section of this specification is divided intotwo parts. Part 1 comprises an overview of the exemplary embodiment ofthis invention. Part 2 comprises an example of the operation of theexemplary embodiment of this invention on a sample Class and sampledata.

1. Overview of the Exemplary Embodiment of this Invention

In this illustrative embodiment of this invention, two JAVA objects arecompared by calling one of the JAVA equality methods,“Comparable.compareTo( )” or “Objects.equals( ),” which are known in theart. While the exemplary embodiment of this invention is described usingJAVA, one skilled in the art will appreciate that this invention can beimplemented in any object-oriented language that supports methodintrospection after studying this specification. According to theexemplary embodiment of this invention, the JAVA equality method“Comparable.compareTo( )” is called if the objects to compare areinstances of Comparable. Otherwise, the “Objects.equals( )” method iscalled. If the selected equality method indicates that there is adifference between the two objects, then get . . . ( ) methods of eachobject are invoked in turn. As will be illustrated further, below, in aget . . . ( ) method, the ellipse represents the name of the method. Theresults of the get . . . ( ) methods are compared. If there aredifferences between the methods, the differences are stored in an XMLdocument. Importantly, the get . . . ( ) method is recursively invokeduntil the Class of the result has no more get . . . ( ) methods todecompose.

This exemplary embodiment of this invention generates an XML documentthat records the difference(s) between the two objects. To show thecontext of a field that is different between the two objects, the fullClass hierarchy is described in XML with attributes showing which partsare different and which parts are the same. Ultimately, the value ofeach field is displayed by a toString( ) call when the type of the fieldcannot be decomposed by further get . . . ( ) calls.

FIG. 1 comprises a Warnier/Orr diagram 100 illustrating the organizationof a process in accordance with an exemplary embodiment of thisinvention. In Warnier/Orr diagram 100, four functions are defined:compare 102, createObjectionDiffNode 104, compareObjects 106 andcreateFieldsNode 108. Each is described in turn.

Compare 102, takes two arguments, Object “a” and Object “b” (the twoobjects to be compared) and returns a document. Compare 102 firstcreates a new document 110. Document compare 102 then sets a node, “n,”to the node returned by createObjectDiffNode 104 (described below). Thenode “n” is then appended to the document Mil 14 and the document isreturned in 116.

The function createObjectDiffNode 104 takes three arguments: a “doc” oftype Document, Object “a” and Object “b” and returns a node. At 118,CreateObjectDiffNode 104 sets a local variable “c” to the returned datafrom a function call to compareObjects 106 (described below). The valueof “c” is checked for various conditions at bracket 120. If the objectsare different classes, then, at 122, this function returns a differentclass element and appends a node with the names of the two classes. Ifone of the objects is null, then the function returns an objectDiffelement with a toString of the non-null object in 124. If both objectsare null, then “null” is returned from the function in 126.

The return node “n” is set to a created objectDiff element 128 and thenObject “a's” class name element is appended to node “n” 130. In 132, if“a” is an instance of a list, then the function createListNode is calledat 134, passing the arguments “doc,” “a” and “b.” If “a” is a string oran instance of a data or an instance of a number at 136, then a stringelement is created in 118. If “a” is anything else at 138, then thecreateFieldsNode 108 function is called. “n” is then returned at 140.

The function compareObjects 106 receives Object “a” and Object “b” asarguments and returns a type integer. This function first checks forexception cases in bracket 142. If, in 144, “a” and “b” are both nullobjects, then “0” is returned. In 146, if one of the two objects isnull, then “ONE_IS_NULL” is returned. If Object “a” and Object “b” aredifferent classes, then “DIFFERENT_CLASSES” is returned in 148.

In bracket 150, one of the JAVA equality methods is run on Object “a”and Object “b.” In 152, if “a” is an instance of a list, then theresults of a call to the compareLists function is returned. If “a” is aninstance of an object of type “Comparable” in 154, then “a” is comparedto “b”. Otherwise, a equals (b) is returned in 156.

The function createFieldsNode 108 receives “doc” of type document, “n”of type node, Object “a” and Object “b” as arguments. First, thisfunction sets the node fields to create a “fields” element in 160. Next,in 162, this function caches the list of methods of Class “a,” whosename starts with “get” and takes no arguments and are not in thegetMethodsToIgnore( ) list and are not “getClass” or “getThis.”

CreateFieldsNode 108 then iterates the method list 164 by invoking themethod on “a” in 166, then invoking the method on “b” in 168. In 170 afield element is created with the attribute name set to the method name.In bracket 172, compareObjects 106 is called on the results of the twomethod invocations 166 and 168 (above). If, in 174, the two methodresults are different, then an attribute is created in the document in176 wherein the attribute is set to “true.” In 178, the functioncreateObjectDiffNode 104 is called. Herein is the recursion in thisexemplary embodiment of this invention. These functions calls tocreateObjectDiffNode 104 repeat until compareObjects 106 returns a valueother than “TRUE.” When compareObjects 106 returns a value other than“TRUE” in 180, an attribute named “different” is set to “false” in 182and a “toString” element is created in 184.

As stated above, an XML document is generated by the above functionsthat describe the differences between the two objects. The XML documenttype definition (DTD) is presented in table I.

TABLE I <?xml version=‘ 1.0’ encoding=‘UTF-8’?> <!ELEMENTobjectDiff(className, (fields I values I list))> <!ELEMENT className(#PCDATA)> <!ELEMENT fileds (field+)> <ELEMENT filed (objectDiff)><!ATTLIST field name CDATA #REQUIRED> <!ELEMENT list (element+ Ireason)> <!ELEMENT element (objectDiff)> <!ATTLIST element index CDATA#REQUIRED> <!ELEMENT values (left, right)> <!ELEMENT left (#PCDATA)><!ELEMENT right (#PCDATA)> <!ELEMENT reason (#PCDATA)>

FIG. 2 is a relational diagram 200 of the elements of Table I. At theinvocation of the differentiation function called herein “objectDiff”202, the class name is listed 204. The fields are then listed 206. Thevalues 208 of the fields are listed in “left” 210 and “right” 212(corresponding to Object “a” and Object “b” above). For multiple fields214, the name is entered 216 and “objectDiff” 202 is recursivelydescribed.

If a list 220 is developed, then the elements 222 are entered into thedocument along with a reason 224. An index 226 of the elements 222 isgenerated and “objectDiff” 202 is recursively called.

2. Example

The following three figures provide an example of the application of theabove-described exemplary embodiment on sample objects of the sameclass. Turning first to FIG. 3, a public class named “Address” is shownas a sample class 300. This object includes five “get . . . ( )”methods: getAge( ) 302, getFirstName( ) 304, getLastName( ) 306,getTitle( ) 308 and getAddress( ) 310.

FIG. 4 illustrates two objects of the class Address 400. In thisexample, they are two simple addresses 402 and 404. FIG. 5 illustratesthe XML document 500 that is generated by the exemplary embodiment ofthis invention. This XML document may be used by, for example, abrowser, which displays the differences in human-readable form.

FIGS. 6-13 comprise a program listing for a sample “objectDiff”function.

It is to be understood that the above-described embodiment is merelyillustrative of the present invention and that many variations of theabove-described embodiment can be devised by one skilled in the artwithout departing from the scope of the invention. It is thereforeintended that such variations be included within the scope of thefollowing claims and their equivalents.

1. A computer implemented method for comparing a first object and asecond object in an object-oriented operating system comprising thesteps of: (a) determining whether the first object is equal to thesecond object; and if said objects are not equal: (b) obtaining one ormore methods from said first object and said second object; (c)determining whether the one or more methods from said first object areequal to the one or more methods from said second object; and (d)recursively performing steps (b) and (c) until all of the methods forthe first object and the second object have been obtained; e) generatinga document comprising a listing of differences between the methods; (f)transforming the document into a human-readable form; and (g) displayingthe human-readable form to a user.
 2. A method in accordance with claim1 wherein step (b) further comprises storing names of said methods, step(c) comprises storing indicia representing the determination whether themethods are the same and step (e) comprises generating a document fromsaid stored names and indicia.
 3. A method in accordance with claim 1wherein said object-oriented operating system comprises JAVA and step(a) comprises running an equality method on said objects.
 4. A method inaccordance with claim 1 wherein said object-oriented operating systemcomprises JAVA and step (c) comprises running an equality method on saidobjects.
 5. A method in accordance with claim 1 wherein human-readableform comprises XML.
 6. A method in accordance with claim 1 whereintransforming the document into human-readable form comprisestransforming the document into a web page.
 7. A method in accordancewith claim 1 wherein said object-oriented operating system comprisesJAVA and step (b) comprises invoking a get . . . ( ) method of eachobject.